/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author PHAMDANH
 */
public class EditCellRenderer extends DefaultTableCellRenderer 
{
    private ShowDataPanel panel;
    public EditCellRenderer(ShowDataPanel panel) 
    {
        this.panel = panel;
    }
    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object value, 
                                                    boolean isSelected, boolean hasFocus, int row, int column) 
    {        
        super.getTableCellRendererComponent(jtable, value, isSelected, hasFocus, row, column);                
        if(value!=null)
        {         
            if(panel.editRowIndex==row)
            {                    
                setBackground(Color.red);
            }
            else
            {
                if(row%2==0)
                    setBackground(Color.white);                
                else
                    setBackground(Color.lightGray);   
                this.setForeground(Color.black);
            }
            if(!"NULL".equals(value.toString()))
            {                
                if(panel.getTable().getColumns().size()>0)
                if(row<panel.getTable().getColumns().get(0).getData().size())
                {     
                    if(panel.getDatabase().SetData(row, column, value, panel.getTable().getName()))
                    {
                        panel.InitTableModel();
                    } 
                    else
                    {                        
                        Object lastValue=panel.getTable().getColumns().get(column).getData().get(row);
                        if(jtable.getModel().getRowCount()>row&&jtable.getModel().getColumnCount()>column)
                            jtable.getModel().setValueAt(lastValue, row, column);                                                     
                    }                
                }
                else
                {
                    if(row==jtable.getRowCount()-1)
                    {
                        if(!panel.hasEditRow)
                        {
                            panel.newEditRow(row);
                            panel.setEditRow(column, value.toString());
                            panel.InitTableModel();
                        }
                    }
                    else
                    {
                        if(panel.hasEditRow)
                            panel.setEditRow(column, value.toString());
                    }                    
                }
            }
            if(isSelected&&panel.hasEditRow&&panel.editRowIndex!=row)
            {
                int nCols=panel.getTable().getColumns().size();
                List<Object> dataRow = new ArrayList<Object>();
                for(int i=0;i<nCols;i++)
                {
                    String val = panel.editRow.get(i);
                    if("NULL".equals(val))
                        dataRow.add(null);
                    else
                    {
                        dataRow.add(val);                    
                    }
                }                    
                panel.getDatabase().InsertRow(dataRow,panel.getTable().getName());
                panel.clearEditRow();                
                panel.InitTableModel();

            }            
        }
        else
        {
            jtable.getModel().setValueAt("NULL", row, column);            
        }        
        return this;
    }
    
}
